package xdu.lz.stage2_design_pattern.chapter9_threadlocal;

public class ThreadLocalTest2 {
    private static final ThreadLocal<String> threadLocal = new ThreadLocal<>();
    public static void main(String[] args) throws InterruptedException {
        threadLocal.set("main");
        Thread t1 = new Thread(()->{
            threadLocal.set("T0");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+" :"+threadLocal.get());
        },"TO");
        Thread t2 = new Thread(()->{
            threadLocal.set("T1");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+" :"+threadLocal.get());
        },"T1");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(Thread.currentThread().getName()+" :"+threadLocal.get());
    }
}
